import axios, { type Method } from 'axios'

const service = axios.create({
  // baseURL: 'https://api-hmzs.itheima.net/api',
  baseURL: 'https://api-hmzs.itheima.net/tj',
  timeout: 5000,
})

service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  },
)

service.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    return Promise.reject(error)
  },
)

type Data<T> ={
  code:number
  msg:string
  data:T
}
export const request =<T>(url:string,method:Method,submitData?:object)=>{
  return service.request<T, Data<T>>({
    url,
    method,
    [method.toLowerCase() === "get" ? "params" : "data"]:submitData
  })
}
interface userInfo{
  token:string
  username:string
}
const testFn = async()=>{
  const res = await request<userInfo>("/login","get",{username:"admin",password:"123456"})
  console.log(res.data.token)
}


export default service
